package com.joizhang.nettylecture.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author joizhang
 */
public class NioTest8 {

    public static void main(String[] args) throws IOException {
        try (FileInputStream inputStream = new FileInputStream("input2.txt");
             FileOutputStream outputStream = new FileOutputStream("output2.txt")) {
            FileChannel inputChannel = inputStream.getChannel();
            FileChannel outputChannel = outputStream.getChannel();

            ByteBuffer buffer = ByteBuffer.allocateDirect(128);

            while (true) {
                buffer.clear();
                int read = inputChannel.read(buffer);
                System.out.println(read);
                if (read == -1) {
                    break;
                }
                buffer.flip();
                outputChannel.write(buffer);
            }
            inputChannel.close();
            outputChannel.close();
        }
    }

}
